<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>jsreport</title>
        <link rel="shortcut icon" href="/img/favicon.ico">
        <meta name="author" content="Jan Blaha">
        <meta name="description" content="jsreport - javascript business reports">
        <style>
            .login-page {
                width: 360px;
                padding: 8% 0 0;
                margin: auto;
            }

            .form {
                position: relative;
                z-index: 1;
                background: #FFFFFF;
                max-width: 360px;
                margin: 0 auto 100px;
                padding: 45px;
                text-align: center;
                box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
            }

            .form input {
                font-family: "Roboto", sans-serif;
                outline: 0;
                background: #f2f2f2;
                width: 100%;
                border: 0;
                margin: 0 0 15px;
                padding: 15px;
                box-sizing: border-box;
                font-size: 14px;
            }

            .form button {
                font-family: "Roboto", sans-serif;
                text-transform: uppercase;
                outline: 0;
                background: #1C97EA;
                width: 100%;
                border: 0;
                padding: 15px;
                color: #FFFFFF;
                font-size: 14px;
                cursor: pointer;
            }

            .form button.auth-server {
                background: #048836;
            }

            .form button:hover, .form button:active, .form button:focus {
                background: #007ACC;
            }

            .form button.auth-server:hover, .form button.auth-server:active, .form button.auth-server:focus {
                background: #025221;
            }

            h1 {
                font-family: "Roboto", sans-serif;
                font-weight: normal;
                margin-top: 0px;
                color: #007ACC;
                font-size: 24px;
            }

            body {
                background: #1C97EA; /* fallback for old browsers */
                background: linear-gradient(to left, #1C97EA, #007ACC);
                font-family: "Roboto", sans-serif;
            }

            <% if (authServer) { %>
                <% if (authServer.autoConnect) { %>
                    body {
                        display: none;
                    }
                <% } %>
            <% } %>
        </style>
    </head>
    <body>
        <div class="login-page">
            <div class="form">
                <h1>jsreport</h1>
                <form class="login-form" id="loginForm" method="POST" autocomplete="off">
                    <input type="text" id="username" name="username" placeholder="username" />
                    <input type="password" name="password" type="password" placeholder="password" autocomplete="off" />

                    <% if (viewModel.login) { %>
                    <div class="form-group" style="color: red;">
                        <strong><%= viewModel.login %></strong>
                    </div>
                    <% } %>

                    <button>login</button>
                </form>
                <% if (authServer) { %>
                <div style="margin-top: 1rem;">
                    <form
                        id="loginFormAuthServer"
                        class="login-form-auth-server"
                        action='/auth-server/login'
                        autocomplete="off"
                        data-auth-server-auto-connect="<%= authServer.autoConnect %>"
                    >
                        <input id="authServerReturnUrl" type="hidden" name="returnUrl" value="" />
                        <button class="auth-server">login with <%= authServer.name %></button>
                    </form>
                </div>
                <% } %>
            </div>
        </div>
        <script>
            var form = document.getElementById("loginForm");
            var formAuthServer = document.getElementById("loginFormAuthServer");
            var authServerAutoConnect = formAuthServer != null && formAuthServer.attributes["data-auth-server-auto-connect"].value === "true"

            var locationPaths = window.location.pathname.split('/')

            if (locationPaths[locationPaths.length - 1] === 'login') {
                form.action = window.location.pathname + window.location.search;
            } else {
                if (window.location.search.indexOf("returnUrl") === -1) {
                    form.action = "<%= options.appPath %>login?returnUrl=" + encodeURIComponent(window.location.pathname + window.location.hash + window.location.search);
                } else {
                    form.action = "<%= options.appPath %>login" + window.location.search;
                }
            }

            if (formAuthServer) {
                formAuthServer.action = "<%= options.appPath %>auth-server/login";

                var returnUrl = getQueryVariable('returnUrl')

                if (returnUrl != null) {
                    authServerReturnUrl.value = returnUrl;
                } else if (locationPaths[locationPaths.length - 1] !== 'login') {
                    // no return url here, we create one
                    authServerReturnUrl.value = window.location.pathname + window.location.hash + window.location.search;
                }
            }

            if (authServerAutoConnect) {
                formAuthServer.submit();
            }

            function getQueryVariable(variable) {
                var query = window.location.search.substring(1);
                var vars = query.split('&');
                for (var i = 0; i < vars.length; i++) {
                    var pair = vars[i].split('=');
                    if (decodeURIComponent(pair[0]) == variable) {
                        if (pair[1] == null) {
                            return '';
                        }

                        return decodeURIComponent(pair[1]);
                    }
                }
            }
        </script>
    </body>
</html>
